package com.bite.messageQueue.mqServer.core;

import com.bite.messageQueue.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 表示一个存储消息的队列
 */
public class MSGQueue {
    //表示队列的标识
    private String name;
    //表示队列是否持久化
    private boolean durable = false;
    //为true时表示这个队列只能被一个消费者消费（独有）
    private boolean exclusive = false;
    //自动删除
    private boolean autoDelete = false;
    //表示扩展参数
    private Map<String, Object> arguments = new HashMap<>();
    //当前队列有哪些消费者订阅
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();
    //记录当前取到了第几个消费者. 方便实现轮询策略.
    private AtomicInteger consumerSeq = new AtomicInteger(0);

    /**
     * 添加消费者
     */
    public ConsumerEnv chooseConsumer() {
        if (consumerEnvList.size() == 0) {
            return null;
        }
        int index = consumerSeq.get() % consumerEnvList.size();
        consumerSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }

    public void addConsumerEnv(ConsumerEnv consumerEnv) {
        consumerEnvList.add(consumerEnv);
    }

    public void removeConsumerEnv(ConsumerEnv consumerEnv) {
        consumerEnvList.remove(consumerEnv);
    }

    public MSGQueue() {
    }

    public MSGQueue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) {
        this.name = name;
        this.durable = durable;
        this.exclusive = exclusive;
        this.autoDelete = autoDelete;
        this.arguments = arguments;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return durable
     */
    public boolean isDurable() {
        return durable;
    }

    /**
     * 设置
     *
     * @param durable
     */
    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    /**
     * 获取
     *
     * @return exclusive
     */
    public boolean isExclusive() {
        return exclusive;
    }

    /**
     * 设置
     *
     * @param exclusive
     */
    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    /**
     * 获取
     *
     * @return autoDelete
     */
    public boolean isAutoDelete() {
        return autoDelete;
    }

    /**
     * 设置
     *
     * @param autoDelete
     */
    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    /**
     * 获取
     *
     * @return arguments
     */
    public String getArguments() {
        //将arguments的Map格式转为JSON格式
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }

    /**
     * 设置
     *
     * @param argumentsJSON
     */
    public void setArguments(String argumentsJSON) {
        //将JSON格式转为Map格式
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJSON, new TypeReference<HashMap<String, Object>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public Object getArguments(String key) {
        return arguments.get(key);
    }

    public void setArguments(String key, Object value) {
        arguments.put(key, value);
    }

    public void setArguments(Map<String, Object> arguments) {
        this.arguments = arguments;
    }

    @Override
    public String toString() {
        return "MSGQueue{name = " + name + ", durable = " + durable + ", exclusive = " + exclusive + ", autoDelete = " + autoDelete + ", arguments = " + arguments + "}";
    }
}
